<?php


namespace app\admin\controller\mall;


use app\admin\controller\BaseController;
use app\common\exception\MessageException;
use app\common\model\mysql\Goods as GoodsModel;
use app\common\model\mysql\GoodsSpecRel as GoodsSpecRelModel;
use app\common\validate\ProductValidate;

class Goods extends BaseController
{
    public function list()
    {
        try {
            $params = $this->request->param();
            $result = GoodsModel::list($params);

            return $this->renderSuccess($result);
        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    public function create()
    {
        if (!$this->request->isPost()) {
            throw new MessageException(['msg' => '请求方法不合法']);
        }
        (new ProductValidate())->goCheck();
        $params = $this->request->param();

        try {
            $model = new GoodsModel;
            if ($model->add($params)) {
                return $this->renderSuccess([], '添加成功');
            }
            return $this->renderFail('添加失败');
        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    public function update()
    {
        if (!$this->request->isPost()) {
            throw new MessageException(['msg' => '请求方法不合法']);
        }

        (new ProductValidate())->goCheck();
        $params = $this->request->param();

        if (!$params['goods_id']) {
            return $this->renderFail('商品不存在');
        }

        try {
            $model = new GoodsModel;
            if ($model->edit($params)) {
                return $this->renderSuccess([], '更新成功');
            }
            return $this->renderFail('更新失败');
        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    public function detail()
    {
        $goodsId = $this->request->param('id');
        if (!isset($goodsId)) {
            throw new MessageException(['msg' => '参数不合法']);
        }

        try {
            $Model = new GoodsModel();
            // 获取商品记录 关联查询(商品图片、sku列表) 'skuList', 'specRel.specValue' 'images.file', 'video', 'videoCover',
            $data = $Model->with(['category', 'skuList'])->find(['goods_id' => $goodsId]);

            // 商品规格列表
            $data['specList'] = GoodsSpecRelModel::getSpecList($data['goods_id']);

            if ($data) {
                return $this->renderSuccess($data, '查询成功');
            }
        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    public function delete()
    {
        $delIds = $this->request->param('ids');
        if (!is_array($delIds)) {
            throw new MessageException(['msg' => '参数不合法']);
        }

        try {
            $Model = new GoodsModel();
            $where = [];
            foreach ($delIds as $key => $id) {
                // 保存数据库记录
                $where[$key] = ['goods_id' => $id, 'status' => config('status.mysql.delete')];
            }
            $data = $Model->saveAll($where);
            // $data = $Model->update(['goods_id' => $params['id'], 'status' => config('status.mysql.delete')]);
            if ($data) {
                return $this->renderSuccess([], '删除成功');
            }

        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }
}